<template>
  <div class="modal" tabindex="-1" role="dialog" aria-hidden="true" :id="id">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">
            <slot name="title"> 更改供应商 </slot>
          </h4>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <form role="form" class="form-horizontal">
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 供应商 </label>
              <div class="col-9" v-if="canShowSelect2 == 1">
                <my-select-supplier
                  :supplierId.sync="supplierId"
                  :supplierType="supplierType"
                  :paymentMethodId.sync="payMethodId"
                  :bspTicketingType.sync="bspTicketingType"
                  :dropdownParentId="id"
                ></my-select-supplier>
              </div>
            </div>
            <div class="row mb-2">
              <label class="control-label col-3 text-end"> 支付方式 </label>
              <div class="col-9">
                <my-select-payment
                  :paymentId.sync="payMethodId"
                ></my-select-payment>
              </div>
            </div>
            <template v-if="bspTicketingType > 0">
              <div class="row mb-2">
                <label class="control-label col-3 text-end">
                  BSP自动开票
                </label>
                <div class="col-8">
                  <div class="form-check form-check-inline">
                    <input
                      class="form-check-input"
                      type="radio"
                      value="0"
                      v-model.number="autoTicketing"
                    />
                    <label class="form-check-label text-danger">不</label>
                  </div>
                  <div class="form-check form-check-inline">
                    <input
                      class="form-check-input"
                      type="radio"
                      value="1"
                      v-model.number="autoTicketing"
                    />
                    <label class="form-check-label">是</label>
                  </div>
                  <br />
                  <span class="text-muted small"
                    >支持国内、单程或往返、成人订单</span
                  >
                </div>
              </div>
              <div class="row" v-if="autoTicketing === 1">
                <label class="control-label col-3 text-end align-center">
                  开票价格
                </label>
                <div class="col-8">
                  <input type="text" class="form-control" v-model="price" />
                  <span class="text-muted small"
                    >为0时，使用订单中的成人票面价</span
                  >
                </div>
              </div>
            </template>
          </form>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary me-3"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import $ from "jquery";
import MySelectSupplier from "@/components/my-select2-supplier.vue";
import MySelectPayment from "@/components/my-select-payment.vue";

export default {
  props: {
    id: {
      type: String,
      default: "updateSupplierModalV2"
    }
  },
  components: {
    MySelectSupplier,
    MySelectPayment,
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "", // 保存promise对象

      supplierId: 0,
      supplierType: -1,
      payMethodId: 0,
      bspTicketingType: 0,
      autoTicketing: 1,
      price: 0,

      canShowSelect2: 0,
      myModal: null,
    };
  },
  watch: {
    supplierId: function () {
      console.log(this.supplierId);
    },
  },
  methods: {
    ok: function () {
      this.hideModal();

      this.resolve({
        supplierId: this.supplierId,
        paymentMethodId: this.payMethodId,
        autoTicketing: this.autoTicketing,
        price: this.price,
      });
    },
    modal: function (supplierId, payMethodId, supplierType, bspTicketingType) {
      this.autoTicketing = 1;
      if (bspTicketingType === undefined) {
        this.bspTicketingType = 0;
      } else {
        this.bspTicketingType = bspTicketingType;
      }

      const self = this;

      const myModalEl = $(this.$el).get(0);
      myModalEl.addEventListener("shown.bs.modal", function () {
        console.log("shown.bs.modal");
        self.canShowSelect2 = 1;

        self.supplierId = supplierId;
        self.payMethodId = payMethodId;
        if (supplierType !== undefined) {
          self.supplierType = supplierType;
        }
      });

      this.showModal();

      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise; // 返回promise对象,给父级组件调用
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl);
      }

      return this.myModal;
    },
  },
};
</script>
